################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Analysis Tax and Benefit System
# Figure B 4
#
#Verena Fetscher
#July 2022
####################



####################
# Load data
####################

load("DataFile_07_Replacement.Rda")
load("replacement.Rda")


##########################
#Illustrations: Single earner
##########################

replacement%>%
  filter(famtype=="single") %>%
  group_by(Country) %>%
  summarise(RR_100 = mean(RR[AW==100],na.rm=T),
            RR_50 = mean(RR[AW==50],na.rm=T),
            RR_200 = mean(RR[AW==200],na.rm=T)) -> df



#Rearrange order in ggplot y-axis
(df %>%
    group_by(Country) %>%    
    arrange(+RR_100))$Country -> order 

df<-reshape(as.data.frame(df), varying=c("RR_50","RR_100","RR_200"), idvar = "group", 
            direction ="long", sep = "_")


##########################
#Figure B.4: Replacement rates. Single, no children, across Western European countries, calculated for 50% AW, AW, 200% AW respectively.
##########################
ggplot(df,aes(RR,Country,color = as.factor(time)))+ 
  geom_point(size=4,aes(color=as.factor(time),shape=as.factor(time)))+
  scale_shape_manual(values=c(16,8,16),
                     name="AW (in %)",
                     guide = guide_legend(reverse = TRUE))+
  scale_color_manual(values=c("lightgray","black","darkgray"),
                     name="AW (in %)",
                     guide = guide_legend(reverse = TRUE))+
  scale_y_discrete(limits = order)+
  ylab("Country") + xlab("Replacement rate") +
  theme_bw()+
  theme(panel.border=element_blank(),
        axis.line=element_line(),
        legend.position="bottom",
        legend.text = element_text(size = 14),
        legend.title = element_text(size=14),
        axis.text=element_text(size=12),
        axis.title=element_text(size=14))

##########################
#Save figure
##########################

ggsave(file="figure_b4.pdf", height = 5.83, width = 8.27, units = "in")

